import { PlayerRank, PlayerPosition } from '@prisma/client'
import { CreatePlayerRequest, UpdatePlayerRequest } from '../types/api'
import { createValidationError } from './errors'

export { createValidationError }

// 验证选手ID
export const validatePlayerId = (id: string): void => {
  if (!id || typeof id !== 'string') {
    throw createValidationError('选手ID不能为空')
  }
  if (id.length < 1 || id.length > 50) {
    throw createValidationError('选手ID长度必须在1-50字符之间')
  }
}

// 验证昵称
export const validateNickname = (nickname: string): void => {
  if (!nickname || typeof nickname !== 'string') {
    throw createValidationError('昵称不能为空')
  }
  if (nickname.length < 1 || nickname.length > 100) {
    throw createValidationError('昵称长度必须在1-100字符之间')
  }
}

// 验证位置
export const validatePositions = (positions: string[]): void => {
  if (!Array.isArray(positions) || positions.length === 0) {
    throw createValidationError('至少需要选择一个位置')
  }
  if (positions.length > 5) {
    throw createValidationError('最多只能选择5个位置')
  }

  const validPositions = Object.values(PlayerPosition)
  for (const position of positions) {
    if (!validPositions.includes(position as PlayerPosition)) {
      throw createValidationError(`无效的位置: ${position}`, {
        validPositions
      })
    }
  }
}

// 验证段位
export const validateRank = (rank: string): void => {
  if (!rank || typeof rank !== 'string') {
    throw createValidationError('段位不能为空')
  }

  const validRanks = Object.values(PlayerRank)
  if (!validRanks.includes(rank as PlayerRank)) {
    throw createValidationError(`无效的段位: ${rank}`, {
      validRanks
    })
  }
}

// 验证最高MMR
export const validateHighestMmr = (mmr: number): void => {
  if (mmr === null || mmr === undefined || isNaN(mmr)) {
    throw createValidationError('最高天梯分不能为空')
  }
  if (mmr < 0) {
    throw createValidationError('天梯分不能小于0')
  }
  if (mmr > 20000) {
    throw createValidationError('天梯分不能大于20000')
  }
  if (!Number.isInteger(mmr)) {
    throw createValidationError('天梯分必须为整数')
  }
}

// 验证能力值
export const validateAbility = (ability: number): void => {
  if (ability === null || ability === undefined || isNaN(ability)) {
    throw createValidationError('能力值不能为空')
  }
  if (ability < 1 || ability > 6) {
    throw createValidationError('能力值必须在1-6星之间')
  }
  if (!Number.isInteger(ability)) {
    throw createValidationError('能力值必须为整数')
  }
}

// 验证词条
export const validateTags = (tags: string[]): void => {
  if (!Array.isArray(tags)) {
    throw createValidationError('词条必须是数组格式')
  }
  
  // 限制词条数量
  if (tags.length > 10) {
    throw createValidationError('词条数量不能超过10个')
  }
  
  // 验证每个词条
  for (const tag of tags) {
    if (typeof tag !== 'string') {
      throw createValidationError('词条必须是字符串')
    }
    if (tag.length < 1 || tag.length > 50) {
      throw createValidationError('词条长度必须在1-50字符之间')
    }
  }
}

// 验证分页参数
export const validatePagination = (page?: number, pageSize?: number): { page: number; pageSize: number } => {
  const validatedPage = page && page > 0 ? Math.floor(page) : 1
  let validatedPageSize = pageSize && pageSize > 0 ? Math.floor(pageSize) : 10
  
  if (validatedPageSize > 100) {
    validatedPageSize = 100
  }

  return { page: validatedPage, pageSize: validatedPageSize }
}

// 验证排序参数
export const validateSort = (sortBy?: string, sortOrder?: string): { sortBy: string; sortOrder: 'asc' | 'desc' } => {
  const validSortFields = ['createdAt', 'updatedAt', 'highestMmr', 'nickname', 'ability']
  const validatedSortBy = sortBy && validSortFields.includes(sortBy) ? sortBy : 'createdAt'
  const validatedSortOrder = sortOrder === 'asc' ? 'asc' : 'desc'

  return { sortBy: validatedSortBy, sortOrder: validatedSortOrder }
}

// 验证创建选手请求
export const validateCreatePlayerRequest = (data: CreatePlayerRequest): void => {
  validatePlayerId(data.id)
  validateNickname(data.nickname)
  validatePositions(data.positions)
  validateRank(data.rank)
  validateHighestMmr(data.highestMmr)
  validateAbility(data.ability)
  validateTags(data.tags)
}

// 验证更新选手请求
export const validateUpdatePlayerRequest = (data: UpdatePlayerRequest): void => {
  if (data.id !== undefined) {
    validatePlayerId(data.id)
  }
  if (data.nickname !== undefined) {
    validateNickname(data.nickname)
  }
  if (data.positions !== undefined) {
    validatePositions(data.positions)
  }
  if (data.rank !== undefined) {
    validateRank(data.rank)
  }
  if (data.highestMmr !== undefined) {
    validateHighestMmr(data.highestMmr)
  }
  if (data.ability !== undefined) {
    validateAbility(data.ability)
  }
  if (data.tags !== undefined) {
    validateTags(data.tags)
  }

  // 至少要有一个字段要更新
  const hasUpdate = data.id !== undefined || 
                   data.nickname !== undefined || 
                   data.positions !== undefined || 
                   data.rank !== undefined || 
                   data.highestMmr !== undefined ||
                   data.ability !== undefined ||
                   data.tags !== undefined

  if (!hasUpdate) {
    throw createValidationError('至少需要提供一个要更新的字段')
  }
}
